
import util from '../../utils/util.js';
import $ from '../../utils/ajax.js';
const app = getApp();
Page({
    data: {
        types: -1,
        topData: {
            name: '',
            tel: '',
            time: '',
            addres: '',
            amount: '',
        },
        shops: {
            id: null,
            startTime: '',
            endTime: '',
            page: 1,
            size: 20,
        },
        shopTime: '',
        amountArr: ['', '服务费', '租赁订单', '销售订单', '用户租赁', '会员充值','会员充值','租赁订单','运营费', '会员充值'],
        count: [],
    },
    onLoad(options) {
        const date = new Date();
        console.log(options.type)
        wx.setNavigationBarTitle({
            title: options.type !== 'undefined' ? '查看运营商' : '查看店铺'
        })
        this.setData({
            types: (options.type === 'undefined' ? -1 : Number(options.type)),
            ['shops.id']: Number(options.id),
            ['shops.startTime']: date.getFullYear() + '-' + ((date.getMonth() - 2) < 10 ? '0' + Math.abs(date.getMonth() - 2) : Math.abs(date.getMonth() - 2)),
            ['shops.endTime']: date.getFullYear() + '-' + ((date.getMonth() + 1) < 10 ? '0' + Math.abs(date.getMonth() + 1) : Math.abs(date.getMonth() + 1)),
            shopTime: date.getFullYear() + '-' + (date.getMonth() + 1)
        }, () => {
            this.storeDetail({ id: options.id });
            this.storeOrderList(this.data.shops)
        })
    },
    storeDetail(data){
        const types = this.data.types;
        $.p(app.wxApp.url + (types === -1 ? 'performance/storeDetail' : 'performance/agentDetail'), { ...data }, app.onStrart, (res) => {
            let arr = {
                tel: res.data.linkPhone,
                time: util.formatTime(res.data.enterTime, 0),
                amount: res.data.amount,
            };
            if (types === -1) {
                arr.name = res.data.storeName;
                arr.addres = res.data.storeAddress;
            } else {
                arr.name = res.data.agentName;
                arr.addres = res.data.agentAddress;
            }
            this.setData({
                topData: arr
            })
            app.onShop();
        }, (res) => {
            util.showToast(res.data)
        });
    },
    storeOrderList(data) {
        const types = this.data.types;
        $.p(app.wxApp.url + (types === -1 ? 'performance/storeOrderList' : 'performance/agentOrderList'), { ...data }, app.onStrart, (res) => {
            let prime;
            if (data.page === 1) {
                prime = [];
            } else {
                prime = this.data.count;
            }
            if (types === -1) {
                for (let item in res.data.list) {
                    prime.push({
                        orderTime: util.formatTime(res.data.list[item].orderTime, 1),
                        commission: res.data.list[item].commission,
                        amount: res.data.list[item].amount,
                        amountType: this.data.amountArr[res.data.list[item].amountType],
                    })
                }
            } else {
                for (let item in res.data.list) {
                    prime.push({
                        storeName: res.data.list[item].storeName,
                        enterTime: util.formatTime(res.data.list[item].enterTime,0),
                        amount: res.data.list[item].amount,
                    })
                }   
            }
            this.setData({
                count: prime
            })
            app.onShop();
        }, (res) => {
            util.showToast(res.data)
        });
    },
    oneTime: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value);
        this.setData({
            ['shops.startTime']: e.detail.value,
            ['shops.page']: 1,
        }, () => {
            this.storeOrderList(this.data.shops);
        })
    },
    twoTime: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            ['shops.endTime']: e.detail.value,
            ['shops.page']: 1,
        }, () => {
            this.storeOrderList(this.data.shops);
        })
    },
    onShow(){
        this.setData({
            ['shops.page']: 1,
        })
    },
    onReachBottom(e) {
        this.setData({
            ['shops.page']: this.data.shops.page + 1
        }, () => {
            this.storeOrderList(this.data.shops);
        })
        
    }
})